package com.mdd.admin.controller;

import com.mdd.admin.aop.Log;
import com.mdd.admin.service.IShopService;
import com.mdd.admin.validate.commons.IdValidate;
import com.mdd.admin.validate.ShopCreateValidate;
import com.mdd.admin.validate.ShopUpdateValidate;
import com.mdd.admin.validate.ShopSearchValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.ShopListedVo;
import com.mdd.admin.vo.ShopDetailVo;
import com.mdd.common.aop.NotPower;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import javax.annotation.Resource;

@RestController
@RequestMapping("api/shop")
@Api(tags = "店铺管理")
public class ShopController {

    @Resource
    IShopService iShopService;

    
    @NotPower
    @GetMapping("/all")
    @ApiOperation(value="所有商户")
    public AjaxResult<List<ShopListedVo>> all() {
        List<ShopListedVo> list = iShopService.all();
        return AjaxResult.success(list);
    }
    
    
    @GetMapping("/list")
    @ApiOperation(value="店铺列表")
    public AjaxResult<PageResult<ShopListedVo>> list(@Validated PageValidate pageValidate,
                                                     @Validated ShopSearchValidate searchValidate) {
        PageResult<ShopListedVo> list = iShopService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="店铺详情")
    public AjaxResult<ShopDetailVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        ShopDetailVo detail = iShopService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "店铺新增")
    @PostMapping("/add")
    @ApiOperation(value="店铺新增")
    public AjaxResult<Object> add(@Validated @RequestBody ShopCreateValidate createValidate) {
        iShopService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "店铺编辑")
    @PostMapping("/edit")
    @ApiOperation(value="店铺编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody ShopUpdateValidate updateValidate) {
        iShopService.edit(updateValidate);
        return AjaxResult.success();
    }
    
    @Log(title = "店铺编辑")
    @PostMapping("/adminEdit")
    @ApiOperation(value="店铺编辑")
    public AjaxResult<Object> adminEdit(@Validated @RequestBody ShopUpdateValidate updateValidate) {
        iShopService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "店铺删除")
    @PostMapping("/del")
    @ApiOperation(value="店铺删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iShopService.del(idValidate.getId());
        return AjaxResult.success();
    }

}
